/*
 * parservalue.c
 *
 *  Created on: 2013-5-19
 *      Author: xkaying
 */

#include "../typedef/value.h"
#include "lexer.h"
#include "parser.h"
#include "parservalue.h"

#include <assert.h>

Value *ParserValue(Lexer *lexer)
{
	assert(lexer);
	int state;
	state = 0;
	Value *pvalue;
	pvalue = 0;
	ptoken=LexerGetToken(lexer);
	while (ptoken) {
		switch (state)
		{
			case 0: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						state = 1;
						pvalue = ValueInit(ptoken->type, 0);
						ValueAppend(pvalue, ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Bin:
					case TokenType_Oct:
					case TokenType_Hex:
					case TokenType_Float:
					case TokenType_Decimal:
					case TokenType_Char:
					case TokenType_String: {
						//input list 读取
						pvalue = ValueInit(ptoken->type, ptoken->name);
						ptoken = LexerGetToken(lexer);
						return pvalue;
					}
					default: {
						return 0;
					}
				}
				printf("parser value状态机错误.状态%d.\n", state);
				return 0;
			}
			case 1: {
				switch (ptoken->type)
				{
					case TokenType_Less: {
						state = 3;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Get: {
						state = 2;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						return pvalue;
					}
				}
				printf("parser value状态机错误.状态%d.\n", state);
				return 0;
			}
			case 2: {
				switch (ptoken->type)
				{
					case TokenType_Variable: {
						state = 1;
						ValueAppend(pvalue, ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						return 0;
					}
				}
				printf("parser value状态机错误.状态%d.\n", state);
				return 0;
			}
			case 3: {
				switch (ptoken->type)
				{
					case TokenType_Variable: {
						state = 4;
						Value *pt;
						LexerBack(lexer);
						pt = ParserValue(lexer);
						if (pt) {
						//	ValueAppendTemplate(pvalue, ptoken->name);
							continue;
						}
						return 0;

					}
					default: {
						return 0;
					}
				}
				printf("parser value状态机错误.状态%d.\n", state);
				return 0;
			}
			case 4: {
				switch (ptoken->type)
				{
					case TokenType_More: {
						state = 5;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comma: {
						state = 3;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						return 0;
					}
				}
				printf("parser value状态机错误.状态%d.\n", state);
				return 0;
			}
			case 5: {
				switch (ptoken->type)
				{
					case TokenType_Get: {
						state = 2;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						return pvalue;
					}
				}
				printf("parser value状态机错误.状态%d.\n", state);
				return 0;
			}
			default: {
				printf("parser value状态机错误.状态default.\n");
			}
		}
	}
	return 0;
}
